package com.hyt.it.ogt.data.service.pj.impl;

import com.hyt.core.base.BaseServiceImpl;
import com.hyt.exception.exception.BusinessLogicException;
import com.hyt.it.ogt.data.mapper.pj.PjDataConfigMapper;
import com.hyt.it.ogt.data.service.pj.IPjDataConfigService;
import com.hyt.it.ogt.pj.common.ResponseCode;
import com.hyt.it.ogt.pj.model.entity.Config;
import com.hyt.it.ogt.pj.utils.MarkConfigProperties;

import cn.hutool.core.collection.CollUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 评卷参数表 服务实现类
 * </p>
 *
 * @author liuqi
 * @date 2022-06-07
 */
@Service
public class PjDataConfigServiceImpl extends BaseServiceImpl<PjDataConfigMapper, Config> implements IPjDataConfigService {

    @Autowired
    private MarkConfigProperties properties;

    @Override
    public Config selectByLabel(String projectId, String label) {
        return this.lambdaQuery()
                .eq(Config::getProjectId, projectId)
                .eq(Config::getParam, label)
                .one();
    }

    @Override
    public List<Config> findProjectConfigs(String projectId) {
        List<Config> configs = this.lambdaQuery()
                .eq(Config::getProjectId, projectId)
                .list();
        return configs;
    }

    @Override
    public Integer initMarkParam(String projectId) {
        // 查询数据库查看该评卷项目的评卷参数是否已经初始化过
        List<Config> configs = this.findProjectConfigs(projectId);
        if (CollUtil.isEmpty(configs)) {
            // 从配置文件中读取评卷参数
            List<Config> newConfigs = new ArrayList<>();
            configs = properties.getConfigs();
            for (Config config : configs) {
                Config con = new Config();
                con.setProjectId(projectId);
                con.setParam(config.getParam());
                con.setValue(config.getValue());
                con.setComment(config.getComment());
                newConfigs.add(con);
            }
            if (CollUtil.isNotEmpty(newConfigs)) {
                this.saveBatch(newConfigs);
            }
        } 
        return ResponseCode.SUCCESS;
    }
}
